﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllenData
{
    public class StreetData
    {
        public Street GetStreet(int streetId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                var street = ctx.Streets.Where(d => d.StreetID == streetId);
                return street.FirstOrDefault();
            }
        }

        public List<Street> GetAllStreets()
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                return ctx.Streets.ToList();
            }
        }

        public int AddNewStreet(Street street)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                ctx.AddToStreets(street);
                ctx.SaveChanges();

                Street newStreet = ctx.Streets.Where(
                    d => d.Name == street.Name).FirstOrDefault();

                if (newStreet == null)
                    return -1;
                else
                    return newStreet.StreetID;
            }
        }

        public void UpdateStreet(Street street)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                Street dbDept = ctx.Streets.First(d => d.StreetID == street.StreetID);
                ctx.ApplyCurrentValues("Streets", street);
                ctx.SaveChanges();
            }
        }

        public void DeleteStreet(int streetId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                Street dbDept = ctx.Streets.First(d => d.StreetID == streetId);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
